// pages/logisticsInfo/logisticsInfo.js
const orderService = require('../../../../api/orderService.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    trackingInfo: {},
    com: '暂无',
    num: '暂无',
    phoneNum: '暂无',
    shipdetails: [],
    distributionPhone: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    let trackData = JSON.parse(this.options.trackData)
    console.log(trackData)
    this.setData({
      trackingInfo: {
        name: trackData.trackingCompany,
        trackId: trackData.trackingNumber
      }
    })

    this.getOrderShipDetails()
  },
  getOrderShipDetails: function () {
    const that = this
    let getData = that.data.trackingInfo
    if (getData.name && getData.trackId) {
      orderService.getOrderShipDetails(getData.name, getData.trackId).then(res => {
        console.log(res)
        if (res) {
          let tempShip = []
          res.shipdetails.forEach(item => {
            tempShip.push(item)
          });
          //快递公司 顺丰 shunfeng,申通 shentong,圆通 yuantong,中通 zhongtong,huitongkuaidi,韵达 yunda,宅急送 zhaijisong, 德邦物流 debangwuliu
          let trackingCompany = ''
          let tempPhoneNum = ''
          if (res.com === 'shunfeng') {
            trackingCompany = '顺丰',
              tempPhoneNum = '95338'
          } else if (res.com === 'shentong') {
            trackingCompany = '申通',
              tempPhoneNum = '95543'
          } else if (res.com === 'yuantong') {
            trackingCompany = '圆通',
              tempPhoneNum = '95554'
          } else if (res.com === 'zhongtong') {
            trackingCompany = '中通',
              tempPhoneNum = '95311'
          } else if (res.com === 'huitongkuaidi') {
            trackingCompany = '汇通',
              tempPhoneNum = '95320'
          } else if (res.com === 'yunda') {
            trackingCompany = '韵达',
              tempPhoneNum = '95546'
          } else if (res.com === 'zhaijisong') {
            trackingCompany = '宅急送',
              tempPhoneNum = '400-6789-000'
          } else if (res.com === 'debangwuliu') {
            trackingCompany = '德邦物流',
              tempPhoneNum = '95353'
          } else {
            trackingCompany = '其他',
              tempPhoneNum = '无'
          }
          that.setData({
            com: trackingCompany,
            num: res.num,
            phoneNum: tempPhoneNum,
            shipdetails: tempShip
          })
        }
      }).catch(e => {
        wx.showToast({
          title: e.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
    let str = 'a15112250161c0755-2451454c18926057890111放0755-4554444分'
    let reg = /((((13[0-9])|(15[^4])|(18[0,1,2,3,5-9])|(17[0-8])|(147))\d{8})|((\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}))?/g
    console.log(str.match(reg))
  },
  callTrackingPhone: function () {
    let phoneNum = this.data.phoneNum
    if (phoneNum !== "暂无") {
      this.handleCallPhone(phoneNum)
    }

  },
  callDistributionPhone: function () {
    let phoneNum = this.data.distributionPhone
    this.handleCallPhone(phoneNum)
  },
  handleCallPhone: function (phoneNum) {
    wx.makePhoneCall({
      phoneNumber: phoneNum
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})